﻿using System;
using UnityEngine;

internal class GUIButton
{
    private static int highestDepthID;
    private static int targetID;

    public static bool Button(Rect bounds, string caption, GUIStyle btnStyle)
    {
        int controlID = GUIUtility.GetControlID(bounds.GetHashCode(), FocusType.Passive);
        int num2 = ((0x3e8 - GUI.depth) * 0x3e8) + controlID;
        bool isActive = false;
        if (Event.current.type == EventType.Layout)
        {
            if (bounds.Contains(Event.current.mousePosition) && (num2 > highestDepthID))
            {
                highestDepthID = num2;
                targetID = controlID;
            }
        }
        else if (Event.current.type == EventType.Repaint)
        {
            isActive = GUIUtility.hotControl == controlID;
            btnStyle.Draw(bounds, new GUIContent(caption), targetID == controlID, isActive, false, false);
            if (targetID == controlID)
            {
                highestDepthID = 0;
                targetID = 0;
            }
        }
        switch (Event.current.GetTypeForControl(controlID))
        {
            case EventType.MouseDown:
                if (targetID == controlID)
                {
                    GUIUtility.hotControl = controlID;
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == controlID)
                {
                    GUIUtility.hotControl = 0;
                }
                return (targetID == controlID);
        }
        return false;
    }
}

